home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11106 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  49 lines

  1. Newsgroups: comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.misc
  2. Path: grizzly.cs.washington.edu!chapman
  3. From: chapman@grizzly.cs.washington.edu (Randy Chapman)
  4. Subject: Re: Are str* functions okay in C++?
  5. Followup-To: comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.misc
  6. X-Newsreader: TIN [version 1.2 PL2]
  7. Sender: news@beaver.cs.washington.edu (USENET News System)
  8. Organization: Computer Science & Engineering, U of Washington, Seattle
  9. Message-ID: <Do61wK.IEG@beaver.cs.washington.edu>
  10. References: <4hpvi0$gt6@news.platinum.com> <Do54G9.4Ly.0.server@indra.com>
  11. X-Nntp-Posting-Host: grizzly.cs.washington.edu
  12. Date: Tue, 12 Mar 1996 17:57:56 GMT
  13.  
  14. Bear Giles (bear@) wrote:
  15. : In article <4hpvi0$gt6@news.platinum.com>,
  16. : Mark Juric <dcmark@platinum.com> wrote:
  17. : >    path = new char[strlen(mbuf)+1];
  18. : >    strcpy(path,mbuf);
  19.  
  20. : One quick note: there's a "strdup()" function which handles this
  21. : for you, although it uses "free" instead of "delete."
  22.  
  23. : Also, another useful technique for this type of code is:
  24.  
  25. : void procedure (argument...)
  26. :    {
  27. :    static char * buffer = 0;
  28.  
  29. :    if (buffer == 0)
  30. :        buffer = malloc (2048);  /* or any _large_ number */
  31.  
  32. :    ...
  33. :    sprintf (buffer, format,...)
  34. :    path = strdup (buffer);
  35. :    ...
  36.  
  37. : The idea is that "buffer" is a captive memory leak -- you allocate a
  38. : large buffer once and never release it.  That increases the runtime
  39. : size of your program, but eliminates the performance hit and possible
  40. : memory leaks from repeatedly allocating and deleting small blocks.
  41.  
  42. However, it limits you to non-multithreaded programs.  Its little things
  43. like this that make trying to use existing code in a multithreaded
  44. environment really hard sometimes =(
  45.  
  46. --randy
  47.  
  48.  
  49.